<?php
/**
 * 付款纪录增加车主id
 * @author angl
 */

date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
    die('error');
}

if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

require(API_ROOT_PATH . '/../inc/common.php');
require(API_ROOT_PATH . '/../inc/Config.php');
require(API_ROOT_PATH . '/../inc/MysqliDb.php');

//初始化数据库链接
function init_db()
{
    $db = new MysqliDb ([
        'host' => MYSQLDB,
        'username' => MYSQLDB_USER,
        'password' => MYSQLDB_PWD,
        'db' => MYSQLDB_DATABASE,
        'port' => 3306,
        'prefix' => 'au_',
        'charset' => 'utf8'
    ]);
    return $db;
}

$db = init_db();
$last_id = 0;
$counter = 0;
while (true){
    $list = $db->where('result',2)->where('transfer_id',$last_id,">")->get('transfer_log',500);
    if(empty($list)){
        logUtil($counter."条数据处理完成",'INFO');
        exit;
    }
    //循环处理
    foreach($list as $item){
        $update_time = $db->where('order_id',$item['order_id'])->where('action_no','4016')->getValue('order_trace_log_list','createtime');
        $update_time = date("Y-m-d H:i:s",$update_time/1000);
        $ret = $db->where('transfer_id',$item['transfer_id'])->update('transfer_log',['last_update'=>$update_time]);
        if($ret){
            logUtil($item['transfer_id']." 处理完成",'INFO');
        }else{
            logUtil($item['transfer_id']." 处理失败",'ERROR');
            exit;
        }
        $last_id =$item['transfer_id'];
        $counter++;
    }
}
